package com.example.demo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.entity.Category;
import com.example.demo.entity.Goods;
import com.example.demo.service.IGoodsService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Goods goods, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
        PageResult pageResult=goodsService.selectGoodsList(goods,pageNum,pageSize);

        return ResponseResult.createBySuccess(200,pageResult,"获取成功");
    }
    @PassToken
    @GetMapping("/list1")
    public ResponseResult list1(Goods goods) {

        List<Goods> list = goodsService.selectGoodsList1(goods);

        return ResponseResult.createBySuccess(200, list, "获取成功");
    }

    /**
     * 导出商品列表
     */

//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Goods goods)
//    {
//        List<Goods> list = goodsService.selectGoodsList(goods);
//        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
//        util.exportExcel(response, list, "商品数据");
//    }

    /**
     * 获取商品详细信息
     */
@PassToken
    @GetMapping(value = "/{gid}")
    public ResponseResult getInfo(@PathVariable("gid") Long gid) {
        Goods goods = goodsService.selectGoodsByGid(gid);
        return ResponseResult.createBySuccess(200, goods, "查询成功");
    }


    @GetMapping(value = "/A/{cid}")
    public ResponseResult getInfo1(@PathVariable("cid") Long cid)
    {
        Goods goods =goodsService.selectGoodsByCid(cid);
        return ResponseResult.createBySuccess(200,goods,"查询成功");
    }

    /**
     * 新增商品
     */

    @PostMapping
    public ResponseResult add(@RequestBody Goods goods){
    int i = goodsService.insertGoods(goods);
        if (i > 0) {
    return ResponseResult.createBySuccess(200, "", "添加成功");
} else {
    return ResponseResult.createByFail(500, "添加失败");
}
}
    /**
     * 修改商品
     */

    @PutMapping
    public ResponseResult edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "更新成功");
        } else {
            return ResponseResult.createByFail(500, "更新失败");
        }
    }

    /**
     * 删除商品
     */

	@DeleteMapping("/{gids}")
    public ResponseResult remove(@PathVariable Long[] gids)
    {
        int i = goodsService.deleteGoodsByGids(gids);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(500, "删除失败");
        }
    }
}
